package org.lys.dao.impl.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.lys.dao.DaoException;
import org.lys.util.JdbcUtils;

public class BaseDaoImpl{
	
	public int executeUpdate(String sql,Object[] params){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			ps = conn.prepareStatement(sql);
			for (int i = 0;params!=null&& i < params.length; i++)
				ps.setObject(i + 1, params[i]);
			return ps.executeUpdate();
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
	}
	public int update(String sql,Object[] params){
		return this.executeUpdate(sql, params);
	}
	public int delete(String sql,Object[] params){
		return this.executeUpdate(sql, params);
	}
	public int add(String sql,Object[] params){
		return this.executeUpdate(sql, params);
	}
	@SuppressWarnings("unchecked")
	public <T> T findForObject(String sql,Object[] params,RowMapper rowMappler){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			ps = conn.prepareStatement(sql);
			for (int i = 0; params!=null&& i< params.length; i++)
				ps.setObject(i + 1, params[i]);
			rs = ps.executeQuery();
			T obj = null;
			if (rs.next()) {
				obj = (T) rowMappler.rowMapper(rs);
			}
			return obj;
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
	}
	public Object addRtnPk(String sql, Object[] params) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			for (int i = 0;params!=null&& i < params.length; i++)
				ps.setObject(i + 1, params[i]);
			ps.executeUpdate();
			rs = ps.getGeneratedKeys();
			if (rs.next())
				return rs.getObject(1);
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public <T> List<T> find(String sql,Object[] params,RowMapper rowMappler){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			ps = conn.prepareStatement(sql);
			for (int i = 0; params!=null&& i< params.length; i++)
				ps.setObject(i + 1, params[i]);
			rs = ps.executeQuery();
			List<T> objList = new ArrayList<T>();
			while (rs.next()) {
				T 	obj = (T) rowMappler.rowMapper(rs);
				objList.add(obj);
			}
			return objList;
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
	}
}
